import java.util.Arrays;

public class Jump {

    int ans = Integer.MAX_VALUE;
    public int jump(int[] nums) {
        int[] dp = new int[nums.length + 1];
        Arrays.fill(dp, -2);
        return process(nums, 0, dp);
    }

    public int process(int[] nums, int i, int[] dp) {
        if (dp[i] != -2) return dp[i];
        if (i == nums.length - 1) {
            dp[i] = 0;
            return dp[i];
        }

        int min = Integer.MAX_VALUE;
        for (int j = 1; j < nums[i] + 1 && (i + j) < nums.length; j++) {
            int k = process(nums, i + j, dp);
            if (k == -1) continue;
            min = Math.min(min, k + 1);
        }
        dp[i] = min == Integer.MAX_VALUE ? -1 : min;
        return dp[i];
    }
}
